Skip to content

Fix for1959#2064

Open
ChrisPulman wants to merge 3 commits intomainfrom
FixFor1959
Open

Fix for1959#2064
ChrisPulman wants to merge 3 commits intomainfrom
FixFor1959

Conversation

@ChrisPulman
Copy link
Member

What kind of change does this PR introduce?

Fix #1959

What is the current behavior?

#1959

What is the new behavior?

This pull request introduces support for correct polymorphic serialization when using System.Text.Json with Refit, ensuring that request bodies are serialized using the declared method parameter type rather than the runtime type. This preserves type discriminators and improves compatibility with JsonPolymorphic and JsonDerivedType attributes. The changes also include new tests to verify this behavior and a refactor of the test HTTP handler for asynchronous operation.

Polymorphic Serialization Improvements

  • Request bodies are now serialized using the declared parameter type, ensuring that System.Text.Json polymorphism features like [JsonPolymorphic] and [JsonDerivedType] are respected and type discriminators are preserved in outgoing payloads (RequestBuilderImplementation.cs).
  • Added documentation to clarify this new behavior in the README.md.

Testing Enhancements

  • Added a new test, RestService_SerializesBodyUsingDeclaredPolymorphicBaseType, to verify that the correct type discriminator is included in serialized request bodies for polymorphic types. This includes new test models and API interfaces.
  • Updated the StubHttpMessageHandler to support asynchronous response delegates, enabling more flexible and accurate test scenarios. [1] [2] [3]

Internal Refactoring

  • Introduced a new generic serialization helper in RequestBuilderImplementation.cs to handle serialization based on the declared body type, using reflection to invoke the correct generic method.

What might this PR break?

Please check if the PR fulfills these requirements

  • Tests for the changes have been added (for bug fixes / features)
  • Docs have been added / updated (for bug fixes / features)

Other information:

Add <IsPackable>false</IsPackable> to example .csproj files to prevent packing sample projects.

Affected files:
- examples/Meow.Common/Meow.Common.csproj
- examples/Meow/Meow.csproj
- examples/SampleUsingLocalApi/ConsoleApplication/ConsoleSampleUsingLocalApi.csproj
- examples/SampleUsingLocalApi/LibraryWithSDKandRefitService/LibraryWithSDKandRefitService.csproj
- examples/SampleUsingLocalApi/RestApiforTest/RestApiForTest.csproj
@codecov
Copy link

codecov bot commented Mar 21, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 82.74%. Comparing base (3f67849) to head (612b87b).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2064      +/-   ##
==========================================
+ Coverage   82.68%   82.74%   +0.06%     
==========================================
  Files          36       36              
  Lines        2731     2742      +11     
  Branches      442      442              
==========================================
+ Hits         2258     2269      +11     
  Misses        371      371              
  Partials      102      102              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Missing type discriminator when using JsonDerivedType

1 participant